package com.tentative.competition.schedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tentative.competition.common.core.result.PageResult;
import com.tentative.competition.common.model.schedule.param.ScheduleOrgInfoAddParam;
import com.tentative.competition.common.model.schedule.resp.ScheduleNewsInfoResp;
import com.tentative.competition.common.model.schedule.resp.ScheduleOrgInfoResp;
import com.tentative.competition.common.model.schedule.vo.FileVo;
import com.tentative.competition.schedule.domain.ScheduleOrgInfo;
import com.tentative.competition.schedule.mapper.ScheduleOrgInfoMapper;
import com.tentative.competition.schedule.service.ScheduleFileInfoService;
import com.tentative.competition.schedule.service.ScheduleOrgInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 赛程组织机构信息 服务实现类
 * </p>
 *
 * @author SmileH
 * @since 2023-06-22
 */
@Service
public class ScheduleOrgInfoServiceImpl extends ServiceImpl<ScheduleOrgInfoMapper, ScheduleOrgInfo> implements IService<ScheduleOrgInfo>, ScheduleOrgInfoService {

    @Lazy
    @Autowired
    private ScheduleFileInfoService fileInfoService;

    @Override
    public PageResult<ScheduleOrgInfoResp> pageList(ScheduleOrgInfoAddParam param) {
        IPage<ScheduleOrgInfoResp> infoResps = baseMapper.pageList(param,new Page<>(param.getPageNum(),param.getPageSize()));
        if (CollUtil.isEmpty(infoResps.getRecords())){
            return new PageResult<>();
        }
        //组装附件信息
        Set<Long> logoFileIds = infoResps.getRecords().stream().map(ScheduleOrgInfoResp::getOrgLogo).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(logoFileIds)){
            Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(logoFileIds);
            infoResps.getRecords().forEach(x->x.setOrgLogoVo(longFileVoMap.get(x.getOrgLogo())));
        }
        PageResult<ScheduleOrgInfoResp> pageResult = new PageResult<>();
        pageResult.setContent(infoResps.getRecords());
        pageResult.setTotal(infoResps.getTotal());
        return pageResult;
    }

    @Override
    public ScheduleOrgInfoResp getInfo(Long id) {
        ScheduleOrgInfoResp resp = new ScheduleOrgInfoResp();
        ScheduleOrgInfo orgInfo = this.lambdaQuery()
                .eq(ScheduleOrgInfo::getId, id)
                .list()
                .stream()
                .findFirst()
                .orElse(null);
        if (ObjUtil.isNotEmpty(orgInfo)){
            BeanUtil.copyProperties(orgInfo,resp);
            //组装附件
            if (ObjUtil.isNotEmpty(resp.getOrgLogo())){
                Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(CollUtil.newHashSet(resp.getOrgLogo()));
                resp.setOrgLogoVo(longFileVoMap.get(resp.getOrgLogo()));
            }
        }
        return resp;
    }

    @Override
    public void addOrEdit(ScheduleOrgInfoAddParam param) {
        ScheduleOrgInfo info = new ScheduleOrgInfo();
        BeanUtil.copyProperties(param,info);
        this.saveOrUpdate(info);
    }

    @Override
    public Boolean deleteOrgInfo(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            throw new RuntimeException("没有需要删除的数据！");
        }
        return this.lambdaUpdate()
                .in(ScheduleOrgInfo::getId,ids)
                .remove();
    }

    @Override
    public List<ScheduleOrgInfoResp> getPreviousWorks(Long scheduleId) {
        List<ScheduleOrgInfo> scheduleOrgInfos = this.lambdaQuery()
                .eq(ScheduleOrgInfo::getScheduleId, scheduleId)
                .list();
        if (CollUtil.isEmpty(scheduleOrgInfos)){
            return new ArrayList<>();
        }
        List<ScheduleOrgInfoResp> orgInfoResps = scheduleOrgInfos.stream().map(x -> {
            ScheduleOrgInfoResp resp = new ScheduleOrgInfoResp();
            BeanUtil.copyProperties(x,resp);
            return resp;
        }).collect(Collectors.toList());
        //组装附件信息
        Set<Long> logoFileIds = orgInfoResps.stream().map(ScheduleOrgInfoResp::getOrgLogo).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(logoFileIds)){
            Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(logoFileIds);
            orgInfoResps.forEach(x->x.setOrgLogoVo(longFileVoMap.get(x.getOrgLogo())));
        }
        return orgInfoResps;
    }
}
